﻿using AIStudio.Core.Models;
using System;
using System.Configuration;

namespace AIStudio.Core
{
    public class LocalSetting: BindableBase, ILocalSetting
    {         
     
        public static string Title { get; set; } = Convert.ToString(ConfigurationManager.AppSettings["Title"]);

        private string _pageTitle;
        public string PageTitle
        {
            get { return _pageTitle; }
            set
            {
                if (_pageTitle != value)
                {
                    SetProperty(ref _pageTitle, value);
                }
            }
        }

        private string _language = ConfigurationManager.AppSettings["Language"];
        public string Language
        {
            get { return _language; }
            set
            {
                if (_language != value)
                {
                    SetProperty(ref _language, value);
                }
            }
        }

        private double _fontsize = Convert.ToDouble(ConfigurationManager.AppSettings["FontSize"]);
        public double FontSize
        {
            get { return _fontsize; }
            set
            {
                if (_fontsize != value)
                {
                    SetProperty(ref _fontsize, value);
                }
            }
        }

        private string _fontfamily = Convert.ToString(ConfigurationManager.AppSettings["FontFamily"]);
        public string FontFamily
        {
            get { return _fontfamily; }
            set
            {
                if (_fontfamily != value)
                {
                    SetProperty(ref _fontfamily, value);
                }
            }
        }

        private string _accent = ConfigurationManager.AppSettings["Accent"];
        public string Accent
        {
            get { return _accent; }
            set
            {
                if (_accent != value)
                {
                    SetProperty(ref _accent, value);
                }
            }
        }

        private string _theme = ConfigurationManager.AppSettings["Theme"];
        public string Theme
        {
            get { return _theme; }
            set
            {
                if (_theme != value)
                {
                    SetProperty(ref _theme, value);
                }
            }
        }

        private string _navigationLoaction = ConfigurationManager.AppSettings["NavigationLocation"];
        public string NavigationLocation
        {
            get { return _navigationLoaction; }
            set
            {
                if (_navigationLoaction != value)
                {
                    SetProperty(ref _navigationLoaction, value);
                }
            }
        }

        private string _navigationAccent = ConfigurationManager.AppSettings["NavigationAccent"];
        public string NavigationAccent
        {
            get { return _navigationAccent; }
            set
            {
                if (_navigationAccent != value)
                {
                    SetProperty(ref _navigationAccent, value);
                }
            }
        }

        private string _titleAccent = ConfigurationManager.AppSettings["TitleAccent"];
        public string TitleAccent
        {
            get { return _titleAccent; }
            set
            {
                if (_titleAccent != value)
                {
                    SetProperty(ref _titleAccent, value);
                }
            }
        }

        private string _toolBarLocation = ConfigurationManager.AppSettings["ToolBarLocation"];
        public string ToolBarLocation
        {
            get { return _toolBarLocation; }
            set
            {
                if (_toolBarLocation != value)
                {
                    SetProperty(ref _toolBarLocation, value);
                }
            }
        }

        public string Version { get; set; } = Convert.ToString(ConfigurationManager.AppSettings["Version"]);

        public string ServerIP { get; set; } = ConfigurationManager.AppSettings["ServerIP"];

        public string UpdateAddress { get; set; } = ConfigurationManager.AppSettings["UpdateAddress"];

        public Action<string> SettingChanged;

        public Action AccentChanged { get; set; }

        public LocalSetting()
        {
            PageTitle = Title;
        }
        
        /// <summary>
        /// 保存appSetting
        /// </summary>
        /// <param name="key">appSetting的KEY值</param>
        /// <param name="value">appSetting的Value值</param>
        public void SetAppSetting(string key, object value)
        {

            //静态类的获取方法
            //if (typeof(LocalSetting).GetProperty(key).GetValue(typeof(LocalSetting)) == value)
            //    return;
            if (this.GetType().GetProperty(key).GetValue(this) == value)
                return;

            // 创建配置文件对象
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (config.AppSettings.Settings[key] != null)
            {
                // 修改
                config.AppSettings.Settings[key].Value = value.ToString();
            }
            else
            {
                // 添加
                AppSettingsSection ass = (AppSettingsSection)config.GetSection("appSettings");
                ass.Settings.Add(key, value.ToString());
            }

            // 保存修改
            config.Save(ConfigurationSaveMode.Modified);

            // 强制重新载入配置文件的连接配置节
            ConfigurationManager.RefreshSection("appSettings");

            //静态类的设置方法
            //typeof(LocalSetting).GetProperty(key).SetValue(typeof(LocalSetting), value);
            this.GetType().GetProperty(key).SetValue(this, value);
            if (SettingChanged != null)
            {
                SettingChanged(key);
            }
            if (AccentChanged != null && (key == "Theme" || key == "Accent"))
            {
                AccentChanged();
            }
        }

        public void SetAppTemplateSetting(string key, object value)
        {
            if (typeof(LocalSetting).GetProperty(key).GetValue(typeof(LocalSetting)) == value)
                return;

            typeof(LocalSetting).GetProperty(key).SetValue(typeof(LocalSetting), value);
            if (SettingChanged != null)
            {
                SettingChanged(key);
            }
        }

        public static void SetSection(string key, ConfigurationSection section)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            //UserSettingSettingSection section = new UserSettingSettingSection();
            //section.Name = "用户布局设置";

            //ToolItems its = new ToolItems();

            //ToolItem it = new ToolItem();
            //it.Original = "error";
            //it.Replacement = "information";

            //its.Add(it);

            //it = new ToolItem();
            //it.Original = "error2";
            //it.Replacement = "information2";

            //its.Add(it);

            //section.ToolItems = its;

            ConfigurationSection lastsection = config.Sections[key];
            if (lastsection != null)
            {
                config.Sections.Remove(key);                
            }
            config.Sections.Add(key, section);
            config.Save();
        }

        public static ConfigurationSection GetSection(string key)
        {
            return System.Configuration.ConfigurationManager.GetSection(key) as ConfigurationSection;
        }
    }


}
